package com.ytf.test.seventh;

/**
 * @author: YTF
 * @date: 2024/9/27 13:48
 * @version: 1.0
 * @description:
 */
public class CADSystem extends Shape {
    private Circle c;
    private Triangle t;
    private Line[] lines = new Line[3];

    public CADSystem(int i) {
        super(i+1);
        for (int j = 0; j < lines.length; j++) {
            lines[j]=new Line(j,j*j);
        }
        c=new Circle(1);
        t= new Triangle(1);
        System.out.println("Combined constructor");
    }

    @Override
    void dispose() {
        System.out.println("CADSystem.dispose()");
        t.dispose();
        c.dispose();
        for (int i = lines.length-1; i >=0 ; i--) {
            lines[i].dispose();
        }
        super.dispose();
    }

    public static void main(String[] args) {
        CADSystem cadSystem = new CADSystem(47);
        try {
            // Code and exception handling...
        }finally {
            cadSystem.dispose();
        }
        /**
         * Output:
         * Shape(int i) constructor
         * Shape(int i) constructor
         * Drawing Line: 0, 0
         * Shape(int i) constructor
         * Drawing Line: 1, 1
         * Shape(int i) constructor
         * Drawing Line: 2, 4
         * Shape(int i) constructor
         * Circle(int i) constructor
         * Shape(int i) constructor
         * Combined constructor
         * CADSystem.dispose()
         * Erasing Triangle
         * Shape dispose
         * Erasing Circle
         * Shape dispose
         * Erasing Line: 2, 4
         * Shape dispose
         * Erasing Line: 1, 1
         * Shape dispose
         * Erasing Line: 0, 0
         * Shape dispose
         * Shape dispose
         *
         * 总结:
         *  调用顺序和清理顺序，调用时先父类（从里到外），清理时先子类（从外到里）。
         */
    }
}
class Shape{
    public Shape(int i) {
        System.out.println("Shape(int i) constructor");
    }

    void dispose(){
        System.out.println("Shape dispose");
    }
}
class Circle extends Shape{

    public Circle(int i) {
        super(i);
        System.out.println("Circle(int i) constructor");
    }

    @Override
    void dispose() {
        System.out.println("Erasing Circle");
        super.dispose();
    }
}
class Triangle extends Shape{

    public Triangle(int i) {
        super(i);
    }

    @Override
    void dispose() {
        System.out.println("Erasing Triangle");
        super.dispose();
    }
}
class Line extends Shape{

    private int start,end;

    public Line(int start, int end) {
        super(start);
        this.start = start;
        this.end = end;
        System.out.println("Drawing Line: "+start+", "+end);
    }

    @Override
    void dispose() {
        System.out.println("Erasing Line: "+start+", "+end);
        super.dispose();
    }
}